package com.zhudeshuai.zhudemolist.activity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;

import com.zhu.zhuCore.utils.LogUtil;
import com.zhudeshuai.zhudemolist.R;
import com.zhudeshuai.zhudemolist.base.BaseActivity;

import java.io.IOException;

/**
 * 音频播放
 *
 * @author zhudeshuai
 * @since 2018/7/10
 */
public class AudioPlayDemoActivity extends BaseActivity {

	private EditText mFileNameEt;
	private String fileName;
	private MediaPlayer mMediaPlayer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_audio_play_demo);
		mFileNameEt = (EditText) findViewById(R.id.et_file_name);


		mMediaPlayer = new MediaPlayer();
		mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			@Override
			public void onCompletion(MediaPlayer mp) {
				LogUtil.d("AudioPlayDemoActivity","播放完成");
			}
		});
		mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
			@Override
			public boolean onError(MediaPlayer mp, int what, int extra) {
				LogUtil.d("AudioPlayDemoActivity","播放失败");
				return true;
			}
		});


	}

	public void onPlay(View view) {
		fileName = mFileNameEt.getText().toString();
		try {
			mMediaPlayer.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +fileName);
			mMediaPlayer.setVolume(1,1);
			mMediaPlayer.prepare();
			mMediaPlayer.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
